//
//  PTSButton.m
//  PTS80
//
//  Created by mac on 15/11/6.
//  Copyright © 2015年 黄杰. All rights reserved.
//

#import "PTSButton.h"

@interface PTSButton ()

@property(nonatomic, strong) UIColor *highLightedColor;
@property(nonatomic, strong) UIColor *selectededColor;
@property(nonatomic, strong) UIColor *enabledColor;

@property(nonatomic, weak) UIActivityIndicatorView *activity;

@property(nonatomic, strong) NSString *BtnTitle;

@end

@implementation PTSButton

- (void)awakeFromNib{
    
    [super awakeFromNib];
    
    self.animationSwitch = YES;
    
    [self setupActivity];
    
    self.layer.anchorPoint = CGPointMake(0.5, 0.5);
    self.layer.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    
}

- (void)setupActivity{
    
    UIActivityIndicatorView *activity =[[UIActivityIndicatorView alloc]init];
    activity.hidesWhenStopped = YES;
    activity.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:activity];
    self.activity = activity;
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.activity attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.activity attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    
}


- (void)setHighlighted:(BOOL)highlighted{
    [super setHighlighted:highlighted];
    if (highlighted) {
        self.highLightedColor = self.highLightedColor ? self.selectededColor : self.backgroundColor;

        if (self.animationSwitch) {
            [self selectedAnimation];
        }
    }
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected) {
        self.backgroundColor = self.selectededColor ? self.selectededColor : self.backgroundColor;
        
        if (self.animationSwitch) {
            [self selectedAnimation];
        }
    }
    
}

- (void)setEnabled:(BOOL)enabled{
    [super setEnabled:enabled];
    
    if (enabled) {
        self.backgroundColor = self.enabledColor ? self.enabledColor : self.backgroundColor;
        
        if (self.animationSwitch) {
            [self enableedAnimation];
        }
    }
    
}

- (void)setuphighLightedColor:(UIColor *)highLightedColor selectededColor:(UIColor *)selectededColor enabledColor:(UIColor *)enabledColor{
    
    self.highLightedColor = highLightedColor;
    self.selectededColor = selectededColor;
    self.enabledColor = enabledColor;
    
}

//选中动画
- (void)selectedAnimation
{
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.fromValue  = [NSValue valueWithCGSize:CGSizeMake(0.5f, 0.5f)];
    scaleAnimation.toValue  = [NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)];//@(0.0f);
    scaleAnimation.springBounciness = (self.springBounciness != 0) ? self.springBounciness : 8.0f;
    scaleAnimation.springSpeed = (self.springSpeed != 0) ? self.springSpeed : 5.0f;
    [self.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}

//不可用动画
- (void)enableedAnimation{
    
    POPSpringAnimation *springAnimation = [POPSpringAnimation animation];
    springAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewCenter];
    springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2)];
    springAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(200, 10)];
    [self pop_addAnimation:springAnimation forKey:@"center"];
    springAnimation.springBounciness = 25;
    springAnimation.springSpeed = 20;
}

//开始加载动画
- (void)startLoading{
    
    self.BtnTitle = self.titleLabel.text;
    
    [self setTitle:@"" forState:UIControlStateNormal];
    
    [self.activity startAnimating];
}

//停止加载动画
- (void)stopLoading{
    
    [self setTitle:self.BtnTitle forState:UIControlStateNormal];
    [self.activity stopAnimating];
    
}

@end
